package hujz.java.file_server;

import hujz.java.file_server._default.LocalDiskStorageImpl;
import hujz.java.file_server._default.MySQLFileMetaSaveImpl;
import hujz.java.file_server.http.DispatcherHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 启动用的配置文件
 */
public class Configuration {

    protected int port;
    protected String path;
    protected String tempDir;
    protected boolean enableSSL;
    protected boolean enableAuthorize;
    protected FileMetaSaveInterface fileMetaSaveImpl;
    protected StorageInterface storageImpl;
    protected IOathInterceptor oathInterceptor;

    public static final Configuration DEAFUALT = new Configuration() {

        {
            this.fileMetaSaveImpl = new MySQLFileMetaSaveImpl();
            this.storageImpl = new LocalDiskStorageImpl();
            ((LocalDiskStorageImpl)this.storageImpl).setStorageDir(System.getProperty("storage.dir", "./"));
            this.port = 9999;
            this.path = "/storage";
            this.tempDir = System.getProperty("storage.tmpdir");
            this.enableSSL = false;
            this.enableAuthorize = false;
            this.oathInterceptor = (ctx, request) -> true;
        }
    };

    public static Configuration INSTANCE = DEAFUALT;

    public int getPort() {
        return port;
    }

    public Configuration port(int port) {
        this.port = port;
        return this;
    }

    public Configuration oathInterceptor(IOathInterceptor oathInterceptor) {
        this.oathInterceptor = oathInterceptor;
        return this;
    }

    public String getPath() {
        return path;
    }

    public Configuration path(String path) {
        this.path = path;
        return this;
    }

    public String getTempDir() {
        return tempDir;
    }

    public Configuration tempDir(String tempDir) {
        this.tempDir = tempDir;
        return this;
    }

    public boolean isEnableSSL() {
        return enableSSL;
    }

    public IOathInterceptor getOathInterceptor() {
        return isEnableAuthorize() ? oathInterceptor : (ctx, request) -> true;
    }

    public Configuration enableSSL(boolean enableSSL) {
        this.enableSSL = enableSSL;
        return this;
    }

    public boolean isEnableAuthorize() {
        return enableAuthorize;
    }

    public Configuration enableAuthorize(boolean enableAuthorize) {
        this.enableAuthorize = enableAuthorize;
        return this;
    }

    public FileMetaSaveInterface getFileMetaSaveImpl() {
        return fileMetaSaveImpl;
    }

    public Configuration fileMetaSaveImpl(FileMetaSaveInterface fileMetaSaveImpl) {
        this.fileMetaSaveImpl = fileMetaSaveImpl;
        return this;
    }

    public StorageInterface getStorageImpl() {
        return storageImpl;
    }

    public Configuration storageImpl(StorageInterface storageImpl) {
        this.storageImpl = storageImpl;
        return this;
    }
}

